<?php
class homeComponents extends sfApplyActionsLibrary //sfComponents
{
    public function executeLoginlq()
    {
        $this->loggedIn = $this->getUser()->isAuthenticated();
        if (!$this->loggedIn)
        {
            $class = sfConfig::get('app_sf_guard_plugin_signin_form',
                    'sfGuardFormSignin');
            $this->formLogin = new $class();
            $this->formLogin->getWidgetSchema()->setFormFormatterName('list');
        }
    }


     public function executeRegisterlq(sfWebRequest $request)
     {
         
         $this->formRegister = new mySfApplyApplyForm();
         
        
              

       // we're getting default or customized applyForm for the task
  //  if( !( ($this->formRegister = $this->newForm( 'applyForm' ) ) instanceof sfGuardUserProfileForm) )
  //  {
      // if the form isn't instance of sfApplyApplyForm, we don't accept it
   //   throw new InvalidArgumentException(
    //      'The custom apply form should be instance of sfApplyApplyForm' );
   // }
/*
    //Code below is used when user is sending his application!
    if( $request->isMethod('post') )
    {
      //gathering form request in one array
      $formValues = $request->getParameter($this->formRegister->getName() );
      if(sfConfig::get('app_recaptcha_enabled') )
      {
        $captcha = array(
          'recaptcha_challenge_field' => $request->getParameter('recaptcha_challenge_field'),
          'recaptcha_response_field'  => $request->getParameter('recaptcha_response_field'),
        );
        //Adding captcha to form array
        $formValues = array_merge( $formValues, array('captcha' => $captcha));
      }
        //binding request form parameters with form
      $this->formRegister->bind($formValues);
      if ($this->formRegister->isValid())
      {

        $guid = "n" . self::createGuid();
        $this->formRegister->getObject()->setValidate( $guid );
        $date = new DateTime();
        $this->formRegister->getObject()->setValidateAt( $date->format( 'Y-m-d H:i:s' ) );
        $this->formRegister->save();
        $confirmation = sfConfig::get( 'app_sfForkedApply_confirmation' );
        if( $confirmation['apply'] )
        {
          try
          {
            
            //Extracting object and sending creating verification mail
            $profile = $this->formRegister->getObject();
           // $this->sendVerificationMail($profile);
            return 'After';
            //echo 'salgo por 1';
            //return $this->redirect( '@register' );
          }
          catch (Exception $e)
          {
            //Cleaning after possible exception thrown in ::sendVerificationMail() method
            $profile = $this->formRegister->getObject();
            $user = $profile->getUser();
            $user->delete();
            //We rethrow exception for the dev environment. This catch
            //catches other than mailer exception, i18n as well. So developer
            //now knows what he's up to.
            if( sfContext::getInstance()->getConfiguration()->getEnvironment() === 'dev' )
            {
              throw $e;
            }
            return 'MailerError';
          }
        }
        else
        {
          $this->activateUser( $this->formRegister->getObject()->getUser() );
          //return $this->redirect( '@home' );
          echo 'salgo por 2';
          return 'After';
        }
      }
      else
      {
          echo 'salgo por 3';
      }
    }*/
 }


static public function createGuid()
  {
    $guid = "";
    // This was 16 before, which produced a string twice as
    // long as desired. I could change the schema instead
    // to accommodate a validation code twice as big, but
    // that is completely unnecessary and would break
    // the code of anyone upgrading from the 1.0 version.
    // Ridiculously unpasteable validation URLs are a
    // pet peeve of mine anyway.
    for ($i = 0; ($i < 16); $i++)
    {
      $guid .= sprintf("%02x", mt_rand(0, 255));
    }
    return $guid;
  }

/*
     public function executeRegisterlq(sfWebRequest $request)
     {
         
         $this->formRegister = new mySfApplyApplyForm();

  
         
     }*/

     public function executeLanguagelq(sfWebRequest $request)
     {
         
         $this->formLanguage = new sfFormLanguage($this->getUser(),array('languages' => array('en', 'es')));
     }

     protected function sendVerificationMail( $profile )
  {
    $this->mail(array('subject' => sfConfig::get('app_sfApplyPlugin_apply_subject',
        sfContext::getInstance()->getI18N()->__("Please verify your account on %1%",
                                                array('%1%' => $this->getRequest()->getHost()), 'sfForkedApply')),
        'fullname' => $profile->getFullname(),
        'email' => $profile->getEmail(),
        'parameters' => array('fullname' => $profile->getFullname(),
                                'validate' => $profile->getValidate()),
        'text' => 'sfApply/sendValidateNewText',
        'html' => 'sfApply/sendValidateNew'));
  }

  /**
   * This function has been overriden. Original used Zend_Mail here. It's used
   * to actually compose and send e-mail verification message.
   * @param array $options
   */
  protected function mail( $options )
  {
    //Checking for all required options
    $required = array('subject', 'parameters', 'email', 'fullname', 'html', 'text');
    foreach ($required as $option)
    {
      if (!isset($options[$option]))
      {
        throw new sfException("Required option $option not supplied to sfApply::mail");
      }
    }
    $message = $this->getMailer()->compose();
    $message->setSubject($options['subject']);

    // Render message parts
    $message->setBody($this->getPartial($options['html'], $options['parameters']), 'text/html');
    $message->addPart($this->getPartial($options['text'], $options['parameters']), 'text/plain');

    //getting information on sender (that's us). May be source of exception.
    $address = $this->getFromAddress();
    $message->setFrom(array($address['email'] => $address['fullname']));
    $message->setTo(array($options['email'] => $options['fullname']));

    //Sending email
    $this->getMailer()->send($message);
  }

}

/*
class mySfApplyApplyForm extends sfApplyApplyForm
{
    private $validate = null;

    public function configure()
    {
        parent::configure();

        $this->removeFields();


        // Add username and password fields which we'll manage
        // on our own. Before you ask, I experimented with separately
        // emitting, merging or embedding a form subclassed from
        // sfGuardUser. It was vastly more work in every instance.
        // You have to clobber all of the other fields (you can
        // automate that, but still). If you use embedForm you realize
        // you've got a nested form that looks like a
        // nested form and an end user looking at that and
        // saying "why?" If you use mergeForm you can't save(). And if
        // you output the forms consecutively you have to manage your
        // own transactions. Adding two fields to the profile form
        // is definitely simpler.


        //Setting username widget
        $this->setWidget( 'username',
                new sfWidgetFormInput( array(), array( 'maxlength' => 16 ) ) );
        $this->widgetSchema->moveField('username', sfWidgetFormSchema::FIRST);

        //Setting password widgets
        $this->setWidget( 'password',
                new sfWidgetFormInputPassword( array(), array('maxlength' => 128) ) );
        $this->widgetSchema->moveField('password', sfWidgetFormSchema::AFTER, 'username');

        $this->setWidget('password2',
                new sfWidgetFormInputPassword( array(), array('maxlength' => 128) ) );
        $this->widgetSchema->moveField('password2', sfWidgetFormSchema::AFTER, 'password');

        //Settings for email fields
        $email = $this->getWidget('email');
        $class = get_class($email);
        $this->setWidget('email2',
                new $class( array(), array('maxlength' => $email->getAttribute('maxlength') )
        ) );
        $this->widgetSchema->moveField( 'email2', sfWidgetFormSchema::AFTER, 'email' );

        //Firstname and lastname
        $this->setWidget( 'firstname', new sfWidgetFormInputText( array(), array( 'maxlength' => 30 ) ) );
        $this->setWidget( 'lastname', new sfWidgetFormInputText( array(), array( 'maxlength' => 70 ) ) );

        $this->widgetSchema->setLabels( array(
            'username' => 'Username',
            'password' => 'Password',
            'password2' => 'Confirm password',
            'email' => 'Email address',
            'email2' => 'Confirm email',
            'firstname' => 'First Name',
            'lastname' => 'Last name'
        ) );

        $this->widgetSchema->setNameFormat('sfApplyApply[%s]');
        $this->widgetSchema->setFormFormatterName('list');

        // We have the right to an opinion on these fields because we
        // implement at least part of their behavior. Validators for the
        // rest of the user profile come from the schema and from the
        // developer's form subclass

        $this->setValidator( 'username', new sfValidatorApplyUsername() );

        $this->setValidator( 'password', new sfValidatorApplyPassword() );
        $this->setValidator( 'password2', new sfValidatorApplyPassword() );

        // Be aware that sfValidatorEmail doesn't guarantee a string that is preescaped for HTML purposes.
        // If you choose to echo the user's email address somewhere, make sure you escape entities.
        // <, > and & are rare but not forbidden due to the "quoted string in the local part" form of email address
        // (read the RFC if you don't believe me...).

        $this->setValidator('email', new sfValidatorAnd( array(
            new sfValidatorEmail( array('required' => true, 'trim' => true) ),
            new sfValidatorString( array('required' => true, 'max_length' => 80) ),
            new sfValidatorDoctrineUnique(
                    array( 'model' => 'sfGuardUserProfile', 'column' => 'email'),
                    array('invalid' => 'An account with that email address already exists. If you have forgotten your password, click "cancel", then "Reset My Password."') )
        )));

        $this->setValidator('email2', new sfValidatorEmail(
                array( 'required' => true, 'trim' => true )));


        $this->setValidator('firstname', new sfValidatorApplyFirstname() );

        $this->setValidator('lastname', new sfValidatorApplyLastname() );

        $schema = $this->validatorSchema;

        // Hey Fabien, adding more postvalidators is kinda verbose!
        $postValidator = $schema->getPostValidator();

        $postValidators = array(
            new sfValidatorSchemaCompare( 'password', sfValidatorSchemaCompare::EQUAL,
                    'password2', array(), array('invalid' => 'The passwords did not match.') ),
            new sfValidatorSchemaCompare( 'email', sfValidatorSchemaCompare::EQUAL,
                    'email2', array(), array('invalid' => 'The email addresses did not match.') ) );

        if( $postValidator )
        {
            $postValidators[] = $postValidator;
        }

        //Include captcha if enabled
        if ($this->isCaptchaEnabled() )
        {
            
            $this->addCaptcha();
        }

        $this->validatorSchema->setPostValidator( new sfValidatorAnd($postValidators) );
    }

    public function doSave($con = null)
    {
        $user = new sfGuardUser();
        $user->setUsername($this->getValue('username'));
        $user->setPassword($this->getValue('password'));
        // They must confirm their account first
        $user->setIsActive(false);
        $user->save();
        $this->userId = $user->getId();

        return parent::doSave($con);
    }

    public function updateObject($values = null)
    {
        $object = parent::updateObject($values);
        $object->setUserId($this->userId);

        // Don't break subclasses!
        return $object;
    }

    protected function isCaptchaEnabled()
    {
        return sfConfig::get('app_recaptcha_enabled');
    }

    protected function addCaptcha()
    {
        $this->widgetSchema['captcha'] = new sfWidgetFormReCaptcha(array(
          'public_key' => sfConfig::get('app_recaptcha_public_key')
        ));

        $this->validatorSchema['captcha'] = new sfValidatorReCaptcha(array(
          'private_key' => sfConfig::get('app_recaptcha_private_key')
        ));
        $this->validatorSchema['captcha']
            ->setMessage('captcha', __('The captcha is not valid (%error%).', array(), 'sfForkedApply'))
            ->setMessage('server_problem', __('Unable to check the captcha from the server (%error%).', array(), 'sfForkedApply'));
    }


    protected function removeFields()
    {

        // We're making a new user or editing the user who is
        // logged in. In neither case is it appropriate for
        // the user to get to pick an existing userid. The user
        // also doesn't get to modify the validate field which
        // is part of how their account is verified by email.

        unset($this['user_id'], $this['validate'], $this['validate_at'],
                $this['created_at'], $this['updated_at'], $this['email_new']);

    }

}*/
?>